// pages/activity/midYearActivity/midYearActivity.js
const app = getApp()
import { requestApi } from '../../../api'
import { formatImage } from '../../../utils/formatImage'
import { formatPrice } from '../../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailData: {},
    channel: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'

    this.data.channel = channel
    this.getDetailData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '【OFashion年中折扣狂欢节】72小时放肆抢购嗨爆全场',
      path: '/pages/activity/midYearActivity/midYearActivity?channel=wx_xcx',
      imageUrl: this.data.detailData.top_pic.new_path,
    }
  },

  /**
   * 跳转至详情页
   */
  toDetail(e) {
    let targetUrl = e.currentTarget.dataset.url

    wx.navigateTo({
      url: targetUrl,
    })
  },

  getDetailData() {
    app.request({
        url: requestApi.midYearActivityUrl,
        login: false,
        data: {},
    }, (res) => {
      let detailData = res.original.data

      if (detailData.top_pic) {
        detailData.top_pic.new_path = detailData.top_pic.path ? formatImage.cdnImage(detailData.top_pic.path, formatImage.THUMBNAIL_L) : ''
        detailData.top_pic.padding_bottom 
        = (detailData.top_pic.height / detailData.top_pic.width * 100)  + '%'
      }

      if (detailData.topic_resource && detailData.topic_resource.length) {
        for (let i in detailData.topic_resource) {
          detailData.topic_resource[i].banner_image.new_path = detailData.topic_resource[i].banner_image.path ? formatImage.cdnImage(detailData.topic_resource[i].banner_image.path, formatImage.THUMBNAIL_L) : ''
          detailData.topic_resource[i].collection_url = '../activityDetail/activityDetail?tId=' + detailData.topic_resource[i].tid + '&channel=' + this.data.channel
        }

        if (detailData.seckill_division) {
          if (detailData.seckill_division.title_image) {
            detailData.seckill_division.title_image.new_path = detailData.seckill_division.title_image.path ? formatImage.cdnImage(detailData.seckill_division.title_image.path, formatImage.THUMBNAIL_L) : ''
            detailData.seckill_division.title_image.padding_bottom = (detailData.seckill_division.title_image.height / detailData.seckill_division.title_image.width * 100) + '%'
          }

          if (detailData.seckill_division.sub_title_image) {
            detailData.seckill_division.sub_title_image.new_path = detailData.seckill_division.sub_title_image.path ? formatImage.cdnImage(detailData.seckill_division.sub_title_image.path, formatImage.THUMBNAIL_L) : ''
            detailData.seckill_division.sub_title_image.padding_bottom = (detailData.seckill_division.sub_title_image.height / detailData.seckill_division.sub_title_image.width * 100) + '%'
          }

          if (detailData.seckill_division.more.image) {
            detailData.seckill_division.more.image.new_path = detailData.seckill_division.more.image.path ? formatImage.cdnImage(detailData.seckill_division.more.image.path, formatImage.THUMBNAIL_L) : ''
            detailData.seckill_division.more.image.padding_bottom = (detailData.seckill_division.more.image.height / detailData.seckill_division.more.image.width * 100) + '%'
          }

          if (detailData.seckill_division.background) {
            detailData.seckill_division.background.new_path = detailData.seckill_division.background.path ? formatImage.cdnImage(detailData.seckill_division.background.path, formatImage.THUMBNAIL_L) : ''
          }

          detailData.seckill_division.more.collection_url = '../activityDetail/activityDetail?tId=' + detailData.seckill_division.more.tid + '&channel=' + this.data.channel

          if (detailData.seckill_division.goods_list && detailData.seckill_division.goods_list.length) {
            for (let j in detailData.seckill_division.goods_list) {
              detailData.seckill_division.goods_list[j].product_cover_image.new_path = detailData.seckill_division.goods_list[j].product_cover_image.path ? formatImage.cdnImage(detailData.seckill_division.goods_list[j].product_cover_image.path, formatImage.THUMBNAIL_L) : ''
              detailData.seckill_division.goods_list[j].new_goods_price = '￥' + formatPrice(detailData.seckill_division.goods_list[j].goods_price / 100)
              detailData.seckill_division.goods_list[j].new_original_price = '￥' + formatPrice(detailData.seckill_division.goods_list[j].original_price / 100)
              detailData.seckill_division.goods_list[j].goods_detail_url = '../../goods/goodsDetail/goodsDetail?gid=' + detailData.seckill_division.goods_list[j].gid + '&channel=' + this.data.channel
            }
          }
        }

        if (detailData.goods_type_list) {
          if (detailData.goods_type_list.title_image) {
            detailData.goods_type_list.title_image.new_path = detailData.goods_type_list.title_image.path ? formatImage.cdnImage(detailData.goods_type_list.title_image.path, formatImage.THUMBNAIL_L) : ''
            detailData.goods_type_list.title_image.padding_bottom = (detailData.goods_type_list.title_image.height / detailData.goods_type_list.title_image.width * 100) + '%'
          }
        }

        if (detailData.goods_collection_list && detailData.goods_collection_list.length) {
          for (let k in detailData.goods_collection_list) {
            detailData.goods_collection_list[k].header_image.new_path = detailData.goods_collection_list[k].header_image.path ? formatImage.cdnImage(detailData.goods_collection_list[k].header_image.path, formatImage.THUMBNAIL_L) :''
            detailData.goods_collection_list[k].header_image.padding_bottom = (detailData.goods_collection_list[k].header_image.height / detailData.goods_collection_list[k].header_image.width * 100) + '%'
            if (detailData.goods_collection_list[k].title_image) {
              detailData.goods_collection_list[k].title_image.new_path = detailData.goods_collection_list[k].title_image.path ? formatImage.cdnImage(detailData.goods_collection_list[k].title_image.path, formatImage.THUMBNAIL_L) : ''
              detailData.goods_collection_list[k].title_image.padding_bottom = (detailData.goods_collection_list[k].title_image.height / detailData.goods_collection_list[k].title_image.width * 100) + '%'
            }

            if (detailData.goods_collection_list[k].data && detailData.goods_collection_list[k].data.length) {
              for (let m in detailData.goods_collection_list[k].data) {
                if (detailData.goods_collection_list[k].data[m].banner_image) {
                  detailData.goods_collection_list[k].data[m].banner_image.new_path = formatImage.cdnImage(detailData.goods_collection_list[k].data[m].banner_image.path, formatImage.THUMBNAIL_L)
                  detailData.goods_collection_list[k].data[m].banner_image.padding_bottom = (detailData.goods_collection_list[k].data[m].banner_image.height / detailData.goods_collection_list[k].data[m].banner_image.width * 100) + '%'
                } else {
                  detailData.goods_collection_list[k].data[m].banner_image = {}
                  detailData.goods_collection_list[k].data[m].banner_image.new_path = '',
                    detailData.goods_collection_list[k].data[m].banner_image.padding_bottom = 0
                }
                detailData.goods_collection_list[k].data[m].collection_url = '../activityDetail/activityDetail?tId=' + detailData.goods_collection_list[k].data[m].tid + '&channel=' + this.data.channel
              }
            }
          }
        }
      }

      this.setData({
        detailData: detailData
      })
    }, (error) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '错误',
      })
    }, () => {
    })
  },

  /**
   * 底部商品瀑布流tab切换
   */
  goodsChange(e) {
    let index = e.currentTarget.getAttribute('id')
    let $goods = document.getElementsByClassName('activity-collection')
    let offsetTop = $goods[0].offsetTop

    this.keywords = index

    if (document.documentElement.scrollTop) {
      document.documentElement.scrollTop = offsetTop
    } else {
      document.body.scrollTop = offsetTop
    }
  }
})